package org.dong.samples.service;

import java.util.List;
import org.dong.samples.model.ExamTest;
import org.dong.samples.model.ExamTestType;
import org.dong.samples.model.PagerModel;
import org.dong.samples.model.Subject;
import org.dong.samples.model.vo.ExamTestTypeVO;
import org.dong.samples.model.vo.ExamTestVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManageTesService {
	
	@Autowired
	private UserSubjectHibernateService userSubjectHiberanteService;
	
	@Autowired
	private TestHibernateService testHiberanteService;
	
	
	public ExamTestVO getTestById(Integer id) throws Exception{
		ExamTest examTest = testHiberanteService.getTestById(id);
		if(examTest != null){
			ExamTestVO vo = new ExamTestVO();
			BeanUtils.copyProperties(examTest, vo);
			//get subject
			Subject subject = userSubjectHiberanteService.getSubjectById(examTest.getSubject().getSubId());
			if(subject != null){
				vo.setSubId(subject.getSubId());
				vo.setSubName(subject.getSubName());
			}
			ExamTestTypeVO typeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(examTest.getExamTestType(), typeVO);
			vo.setExamTestType(typeVO);
			return vo;
		}
		return null;
	}
	
	public List<Subject> findSubject() throws Exception{
		List<Subject> subList = userSubjectHiberanteService.findAllSubject();
		return subList;
	}
	
	public List<ExamTestType> findExamTestType() throws Exception{
		return testHiberanteService.findExamTestType();
	}
	
	public void deleteTest(Integer id) throws Exception{
		testHiberanteService.deleteTest(id);
	}

	public List<ExamTest> loadAllTest() throws Exception {
		return testHiberanteService.findAllTest();
	}
	
	public void saveExamTest(ExamTest test) throws Exception{
		testHiberanteService.createTest(test);
	}
	
	public void editExamTest(ExamTest test) throws Exception{
		testHiberanteService.editTest(test);
	}
	
	public PagerModel<ExamTestVO> pageFindExamTest() throws Exception{
		return testHiberanteService.pageFindExamTest();
	}
}
